package com.nanhai.buddhist.college.module.collegebase.dal.mysql.professions;

import java.util.*;


import cn.hutool.core.bean.BeanUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.datapermission.core.annotation.DataPermission;
import com.nanhai.buddhist.college.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.nanhai.buddhist.college.framework.mybatis.core.mapper.BaseMapperX;
import com.nanhai.buddhist.college.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.professions.vo.ProfessionsExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.professions.vo.ProfessionsPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.colleges.CollegesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.departments.DepartmentsDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.professions.ProfessionIdAndCollegeIdDo;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.professions.ProfessionsCountDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.professions.ProfessionsDO;
import org.apache.ibatis.annotations.Mapper;


/**
 * 学校管理--专业 Mapper
 *
 * @author 开发
 */
@Mapper
public interface ProfessionsMapper extends BaseMapperX<ProfessionsDO> {

    default PageResult<ProfessionsDO> selectPage(ProfessionsPageReqVO reqVO) {
        return selectPageForJoin(reqVO, ProfessionsDO.class, new MPJLambdaWrapperX<ProfessionsDO>()
                .likeIfPresent(ProfessionsDO::getProfessionName, reqVO.getProfessionName())
                .eqIfPresent(ProfessionsDO::getProfessionCode, reqVO.getProfessionCode())
                .eqIfPresent(ProfessionsDO::getDepartmentsId, reqVO.getDepartmentsId())
                .eqIfPresent(CollegesDO::getId, reqVO.getCollegeId())
                .selectAll(ProfessionsDO.class)
                .selectAs(DepartmentsDO::getDepartmentName, ProfessionsDO::getDepartName)
                .leftJoin(DepartmentsDO.class, DepartmentsDO::getId, ProfessionsDO::getDepartmentsId)
                .selectAll(DepartmentsDO.class)
                .selectAs(CollegesDO::getCollegeName, ProfessionsDO::getCollegeName)
                .leftJoin(CollegesDO.class, CollegesDO::getId, DepartmentsDO::getCollegeId)
                .orderByDesc(ProfessionsDO::getCreateTime));
    }

    default List<ProfessionsDO> selectList(ProfessionsExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ProfessionsDO>()
                .likeIfPresent(ProfessionsDO::getProfessionName, reqVO.getProfessionName())
                .eqIfPresent(ProfessionsDO::getProfessionCode, reqVO.getProfessionCode())
                .eqIfPresent(ProfessionsDO::getDepartmentsId, reqVO.getDepartmentsId())
                .orderByDesc(ProfessionsDO::getCreateTime));
    }


    default List<ProfessionsCountDO> selectCountByDepartIds(Collection<String> departmentIds) {
        MPJLambdaWrapper<ProfessionsDO> in = new MPJLambdaWrapperX<ProfessionsDO>()
                .selectCount(ProfessionsDO::getId, ProfessionsCountDO::getProfessionCount)
                .selectAs(ProfessionsDO::getDepartmentsId, ProfessionsCountDO::getDepartmentId)
                .groupBy(ProfessionsDO::getDepartmentsId).in(ProfessionsDO::getDepartmentsId, departmentIds);

        List<Map<String, Object>> maps1 = selectMaps(in);
        return BeanUtil.copyToList(maps1, ProfessionsCountDO.class);
    }




    @DataPermission(enable = false)
    default List<ProfessionIdAndCollegeIdDo> getIdsByCollege(Collection<String> collegeIds){
        MPJLambdaWrapper<ProfessionIdAndCollegeIdDo> professionsDOMPJLambdaWrapper = new MPJLambdaWrapperX<ProfessionIdAndCollegeIdDo>()
                .inIfPresent(CollegesDO::getId, collegeIds)
                .selectAs(CollegesDO::getId,ProfessionIdAndCollegeIdDo::getCollegeId)
                .selectAs(ProfessionsDO::getId,ProfessionIdAndCollegeIdDo::getProfessionId)
                .leftJoin(DepartmentsDO.class, DepartmentsDO::getId, ProfessionsDO::getDepartmentsId)
                .leftJoin(CollegesDO.class, CollegesDO::getId, DepartmentsDO::getCollegeId);
        return selectJoinList(ProfessionIdAndCollegeIdDo.class,professionsDOMPJLambdaWrapper);
    }
}
